package com.yykj.pay.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yykj.common.enums.ResultEnums;
import com.yykj.common.vo.ResultVO;
import com.yykj.pay.server.entity.ClientPayConfig;
import com.yykj.pay.server.param.PageParam;
import com.yykj.pay.server.service.ClientPayConfigService;
import com.yykj.pay.server.util.Page.CommonPage;
import com.yykj.pay.server.util.Page.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author zeng
 * @Date 2022/4/8
 */
@RestController
@RequestMapping("/clientPayConfig")
@Api(tags = "支付信息配置")
@AllArgsConstructor
@Slf4j
public class ClientPayConfigController {

    final ClientPayConfigService clientPayConfigService;

    @PostMapping("/create")
    @ApiOperation(value = "添加商户应用支付配置")
    public ResultVO<T> create(@RequestBody @Valid ClientPayConfig payConfig) {
        return ResultVO.success(clientPayConfigService.insertClientPayConfig(payConfig));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新商户应用支付配置")
    public ResultVO<T> update(@RequestBody @Valid ClientPayConfig payConfig) {
        if (StringUtils.isEmpty(payConfig.getPayConfigId())){
            return ResultVO.error(ResultEnums.NOT_EXIST);
        }
        return ResultVO.success(clientPayConfigService.updateById(payConfig));
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取商户应用列表")
    public ResultVO<ClientPayConfig> page(@RequestBody PageParam pageParam) {

        String orderByField = pageParam.getOrderByField();
        String orderBy = StringUtils.isEmpty(orderByField) ? "created_time" : StrUtil.toUnderlineCase(orderByField);
        PageUtils.startPage(pageParam.getPageNum(),pageParam.getPageSize(),orderBy,pageParam.getOrderType());
        return ResultVO.success(CommonPage.restPage(clientPayConfigService.list(
                new LambdaQueryWrapper<ClientPayConfig>()
                        .eq(ClientPayConfig::getIsDeleted,0)))
        );
    }

    @GetMapping("/setStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name="payConfigId",value = "商户应用主键ID",required = true),
            @ApiImplicitParam(name="status",value = "0 : 禁用 1：启用",required = true),
    })
    @ApiOperation(value = "设置商户应用支付配置启用状态")
    public ResultVO<T> setStatus(@RequestParam("payConfigId") String payConfigId,@RequestParam("status") Integer status) {
        ClientPayConfig payConfig = clientPayConfigService.getById(payConfigId);
        if (payConfig == null){
            return ResultVO.error(ResultEnums.NOT_EXIST);
        }
        payConfig.setStatus(status);
        return ResultVO.success(clientPayConfigService.updateById(payConfig));
    }

    @GetMapping("/deleted")
    @ApiOperation(value = "删除商户应用支付配置")
    public ResultVO<T> deleted(@RequestParam("payConfigId") String payConfigId) {
        ClientPayConfig payConfig = clientPayConfigService.getById(payConfigId);
        if (payConfig == null){
            return ResultVO.error(ResultEnums.NOT_EXIST);
        }
        payConfig.setIsDeleted(1);
        return ResultVO.success(clientPayConfigService.updateById(payConfig));
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取医院配置")
    public ResultVO<T> get(@RequestParam("hospitalId") String hospitalId) {
        ClientPayConfig payConfig = clientPayConfigService.getByClientId(hospitalId);
        if (payConfig == null){
            return ResultVO.error(ResultEnums.NOT_EXIST);
        }
        return ResultVO.success(payConfig);
    }

}
